//
//  KPNavigationAccessoryView.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/4/3.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit
import Eureka

class KPNavigationAccessoryView: NavigationAccessoryView {

    var fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    var confirmButton = UIBarButtonItem.init(title: "确定", style: .plain, target: nil, action: nil)

    public override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 44.0))
        autoresizingMask = .flexibleWidth
        fixedSpace.width = 22.0
        initializeChevrons()
        setItems([previousButton, fixedSpace, nextButton, flexibleSpace, confirmButton], animated: false)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    private func initializeChevrons() {
        var bundle = Bundle(for: NavigationAccessoryView.self)
        if let resourcePath = bundle.path(forResource: "Eureka", ofType: "bundle") {
            if let resourcesBundle = Bundle(path: resourcePath) {
                bundle = resourcesBundle
            }
        }

        var imageLeftChevron = UIImage(named: "back-chevron", in: bundle, compatibleWith: nil)
        var imageRightChevron = UIImage(named: "forward-chevron", in: bundle, compatibleWith: nil)
        // RTL language support
        if #available(iOS 9.0, *) {
            imageLeftChevron = imageLeftChevron?.imageFlippedForRightToLeftLayoutDirection()
            imageRightChevron = imageRightChevron?.imageFlippedForRightToLeftLayoutDirection()
        }

        previousButton = UIBarButtonItem(image: imageLeftChevron, style: .plain, target: nil, action: nil)
        nextButton = UIBarButtonItem(image: imageRightChevron, style: .plain, target: nil, action: nil)
    }

}

